#include "Utils.h"



Utils Utils::mInstance;

Utils::Utils()
{

}

Utils *Utils::getInstance()
{
    return &mInstance;
}


uint8_t Utils::calcSum(uint8_t *src, uint16_t len)
{
    uint8_t sum  = 0;

    for(int i =0; i < len; i++){
        sum  += src[i];
    }

    return sum;
}

void Utils::hex2text(uint8_t hex, char *text)
{
    uint8_t halfHex  = hex >> 4;

    if(halfHex >9){
        text[0]  = halfHex -10 + 'a';
    }
    else{
        text[0]  = halfHex + '0';
    }

    halfHex  = hex & 0x0f;

    if(halfHex >9){
        text[1]  = halfHex -10 + 'a';
    }
    else{
        text[1]  = halfHex + '0';
    }

    text[2]  = '\0';
}


void Utils::hex2String(uint8_t hex, QString &text)
{
    uint8_t halfHex  = hex >> 4;

    text.clear();

    if(halfHex >9){
        text.append( halfHex -10 + 'a' );
    }
    else{
        text.append( halfHex + '0');
    }

    halfHex  = hex & 0x0f;

    if(halfHex >9){
        text.append(halfHex -10 + 'a');
    }
    else{
        text.append(halfHex + '0');
    }
}

void Utils::hexArray2String(uint8_t *hex, uint16_t size, QString &string)
{
    QString tmp;
    string.clear();

    for(int i = 0; i < size; i++){
        hex2String(hex[i], tmp);
        string += tmp;
        string.append(' ');
    }
}
